<template>
  <page-header-wrapper>
    <template v-slot:content>
      <div class="page-header-content">
        <div class="avatar">
          <a-avatar size="large" :src="avatar" />
        </div>
        <div class="content">
          <div class="content-title">
            {{ timeFix() }}，{{ userName }}<span class="welcome-text">，{{ welcome }}</span>
          </div>
          <div>无人机配送管理平台 | {{ userRole }}</div>
        </div>
      </div>
    </template>
    <template v-slot:extraContent>
      <div class="extra-content">
        <div class="stat-item">
          <a-statistic title="今日订单" :value="56" />
        </div>
        <div class="stat-item">
          <a-statistic title="在线无人机" :value="8" :suffix="`/ 24`" />
        </div>
        <div class="stat-item">
          <a-statistic title="配送完成率" :value="92" suffix="%" />
        </div>
      </div>
    </template>

    <div>
      <a-row :gutter="24">
        <a-col :xl="16" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            :loading="loading"
            style="margin-bottom: 24px;"
            :bordered="false"
            title="进行中的订单"
            :body-style="{ padding: 0 }"
          >
            <a slot="extra" @click="viewAllOrders">全部订单</a>
            <div>
              <a-card-grid class="project-card-grid" :key="i" v-for="(item, i) in activeOrders">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a-avatar size="small" :style="{backgroundColor: getOrderTypeColor(item.type)}">{{ item.type.charAt(0) }}</a-avatar>
                      <a @click="viewOrderDetail(item)">{{ item.orderSn }}</a>
                    </div>
                    <div slot="description" class="card-description">
                      {{ item.address }}
                    </div>
                  </a-card-meta>
                  <div class="project-item">
                    <a-tag :color="getStatusColor(item.status)">{{ item.status }}</a-tag>
                    <span class="datetime">{{ item.createTime }}</span>
                  </div>
                </a-card>
              </a-card-grid>
            </div>
          </a-card>

          <a-card :loading="loading" title="配送动态" :bordered="false">
            <a-list>
              <a-list-item :key="index" v-for="(item, index) in activities">
                <a-list-item-meta>
                  <a-avatar slot="avatar" size="small" :src="item.user.avatar" />
                  <div slot="title">
                    <span>{{ item.user.nickname }}</span>&nbsp;
                    {{ item.action }}&nbsp;
                    <a href="javascript:;">{{ item.target }}</a>
                  </div>
                  <div slot="description">{{ item.time }}</div>
                </a-list-item-meta>
              </a-list-item>
            </a-list>
          </a-card>
        </a-col>
        <a-col
          style="padding: 0 12px"
          :xl="8"
          :lg="24"
          :md="24"
          :sm="24"
          :xs="24">
          <a-card
            title="快速开始 / 便捷导航"
            style="margin-bottom: 24px"
            :bordered="false"
            :body-style="{ padding: 0 }"
          >
            <div class="item-group">
              <a @click="createOrder">创建订单</a>
              <a @click="viewDrones">无人机管理</a>
              <a @click="viewMap">配送地图</a>
              <a @click="viewLiveMonitor">实时监控</a>
              <a @click="viewReports">数据报表</a>
              <a @click="viewSettings">系统设置</a>
            </div>
          </a-card>
          <a-card
            title="无人机状态"
            style="margin-bottom: 24px"
            :loading="loading"
            :bordered="false"
          >
            <div>
              <a-row :gutter="[16, 16]">
                <a-col :span="12" v-for="(drone, index) in drones" :key="index">
                  <a-card size="small" :bordered="false" :bodyStyle="{padding: '12px'}">
                    <div class="drone-card">
                      <div class="drone-title">
                        <a-badge :status="getDroneStatus(drone.status)" :text="drone.id" />
                      </div>
                      <div class="drone-info">
                        <p>电量: {{ drone.battery }}%</p>
                        <a-progress :percent="drone.battery" size="small" :strokeColor="getBatteryColor(drone.battery)" />
                        <p>状态: {{ drone.status }}</p>
                        <p v-if="drone.orderSn">订单: {{ drone.orderSn }}</p>
                      </div>
                      <div class="drone-actions">
                        <a @click="viewDroneDetail(drone)">详情</a>
                        <a-divider type="vertical" />
                        <a @click="viewLiveVideo(drone)">监控</a>
                      </div>
                    </div>
                  </a-card>
                </a-col>
              </a-row>
            </div>
          </a-card>
          <a-card
            title="告警信息"
            :bordered="false"
            :body-style="{ padding: '0 32px 40px 32px' }"
          >
            <a-list size="large">
              <a-list-item :key="i" v-for="(item, i) in alerts">
                <a-list-item-meta>
                  <div slot="title">
                    <a-icon :type="getAlertIcon(item.level)" :style="{color: getAlertColor(item.level)}" />
                    {{ item.title }}
                  </div>
                  <div slot="description">
                    <div>{{ item.content }}</div>
                    <div class="alert-time">{{ item.time }}</div>
                  </div>
                </a-list-item-meta>
                <template slot="actions">
                  <a @click="handleAlert(item)">处理</a>
                  <a @click="ignoreAlert(item)">忽略</a>
                </template>
              </a-list-item>
            </a-list>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </page-header-wrapper>
</template>

<script>
import { timeFix } from '@/utils/util'

export default {
  name: 'Workplace',
  data () {
    return {
      loading: false,
      welcome: '欢迎使用无人机配送管理系统',
      avatar: '',
      userName: '管理员',
      userRole: '系统管理员',
      activeOrders: [
        {
          id: 'DD20230001',
          orderSn: 'DD20230001',
          type: '普通配送',
          status: '配送中',
          address: '北京市朝阳区三里屯SOHO',
          createTime: '1小时前',
          receiverName: '张三',
          receiverPhone: '13800138000',
          droneId: 'UAV-001'
        },
        {
          id: 'DD20230002',
          orderSn: 'DD20230002',
          type: '加急配送',
          status: '待分配',
          address: '北京市海淀区中关村软件园',
          createTime: '2小时前',
          receiverName: '李四',
          receiverPhone: '13900139000',
          droneId: null
        },
        {
          id: 'DD20230003',
          orderSn: 'DD20230003',
          type: '普通配送',
          status: '配送中',
          address: '北京市西城区金融街',
          createTime: '3小时前',
          receiverName: '王五',
          receiverPhone: '13700137000',
          droneId: 'UAV-002'
        },
        {
          id: 'DD20230004',
          orderSn: 'DD20230004',
          type: '特殊配送',
          status: '待取件',
          address: '北京市东城区王府井',
          createTime: '4小时前',
          receiverName: '赵六',
          receiverPhone: '13600136000',
          droneId: null
        }
      ],
      activities: [
        {
          user: {
            nickname: '系统',
            avatar: ''
          },
          action: '分配无人机',
          target: 'UAV-001',
          time: '10分钟前'
        },
        {
          user: {
            nickname: '张三',
            avatar: ''
          },
          action: '创建订单',
          target: 'DD20230005',
          time: '30分钟前'
        },
        {
          user: {
            nickname: '李四',
            avatar: ''
          },
          action: '完成配送',
          target: 'DD20230000',
          time: '1小时前'
        },
        {
          user: {
            nickname: '系统',
            avatar: ''
          },
          action: '安排维护',
          target: 'UAV-005',
          time: '2小时前'
        }
      ],
      drones: [
        {
          id: 'UAV-001',
          status: '飞行中',
          battery: 75,
          orderSn: 'DD20230001'
        },
        {
          id: 'UAV-002',
          status: '飞行中',
          battery: 60,
          orderSn: 'DD20230003'
        },
        {
          id: 'UAV-003',
          status: '待机中',
          battery: 100,
          orderSn: null
        },
        {
          id: 'UAV-004',
          status: '充电中',
          battery: 35,
          orderSn: null
        }
      ],
      alerts: [
        {
          id: 1,
          title: '无人机电量低',
          level: 'error',
          content: 'UAV-003电量低于20%，请及时安排充电',
          time: '5分钟前'
        },
        {
          id: 2,
          title: '订单延迟',
          level: 'warning',
          content: '订单DD20230002已超过预定配送时间',
          time: '10分钟前'
        },
        {
          id: 3,
          title: '系统通知',
          level: 'info',
          content: '系统将于今晚22:00-23:00进行例行维护',
          time: '1小时前'
        }
      ]
    }
  },
  mounted () {
    this.loadData()
    this.avatar = 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png'
  },
  methods: {
    timeFix,
    loadData () {
      this.loading = true
      setTimeout(() => {
        this.loading = false
      }, 1000)
    },
    getOrderTypeColor (type) {
      const colorMap = {
        '普通配送': '#1890ff',
        '加急配送': '#f5222d',
        '特殊配送': '#722ed1'
      }
      return colorMap[type] || '#1890ff'
    },
    getStatusColor (status) {
      const colorMap = {
        '待分配': 'blue',
        '待取件': 'purple',
        '配送中': 'green',
        '已完成': 'cyan',
        '已取消': 'red'
      }
      return colorMap[status] || 'default'
    },
    getDroneStatus (status) {
      const statusMap = {
        '飞行中': 'processing',
        '待机中': 'success',
        '充电中': 'warning',
        '维修中': 'error'
      }
      return statusMap[status] || 'default'
    },
    getBatteryColor (battery) {
      if (battery >= 60) return '#52c41a'
      if (battery >= 20) return '#faad14'
      return '#f5222d'
    },
    getAlertIcon (level) {
      const iconMap = {
        'error': 'exclamation-circle',
        'warning': 'warning',
        'info': 'info-circle'
      }
      return iconMap[level] || 'info-circle'
    },
    getAlertColor (level) {
      const colorMap = {
        'error': '#f5222d',
        'warning': '#faad14',
        'info': '#1890ff'
      }
      return colorMap[level] || '#1890ff'
    },
    viewAllOrders () {
      this.$router.push('/list/basic-list')
    },
    viewOrderDetail (order) {
      this.$message.info(`查看订单详情: ${order.orderSn}`)
      // 实际项目中可以跳转到订单详情页
      // this.$router.push(`/list/basic-list/${order.id}`)
    },
    createOrder () {
      this.$router.push('/list/basic-list')
    },
    viewDrones () {
      this.$router.push('/list/table-list')
    },
    viewDroneDetail (drone) {
      this.$message.info(`查看无人机详情: ${drone.id}`)
    },
    viewLiveVideo (drone) {
      this.$router.push({
        path: '/device/live-broadcast',
        query: { droneId: drone.id }
      })
    },
    viewMap () {
      this.$router.push('/map/implementation')
    },
    viewLiveMonitor () {
      this.$router.push('/device/live-broadcast')
    },
    viewReports () {
      this.$message.info('查看数据报表')
    },
    viewSettings () {
      this.$message.info('系统设置')
    },
    handleAlert (alert) {
      this.$message.info(`正在处理告警: ${alert.title}`)
    },
    ignoreAlert (alert) {
      this.$message.info(`已忽略告警: ${alert.title}`)
      this.alerts = this.alerts.filter(item => item.id !== alert.id)
    }
  }
}
</script>

<style lang="less" scoped>
@import './Workplace.less';

.project-list {
  .project-card-grid {
    width: 33.33%;

    @media (max-width: 1200px) {
      width: 50%;
    }

    @media (max-width: 768px) {
      width: 100%;
    }
  }

  .card-title {
    font-size: 0;

    a {
      color: rgba(0, 0, 0, 0.85);
      margin-left: 12px;
      line-height: 24px;
      height: 24px;
      display: inline-block;
      vertical-align: top;
      font-size: 14px;

      &:hover {
        color: #1890ff;
      }
    }
  }

  .card-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }

  .project-item {
    display: flex;
    margin-top: 8px;
    overflow: hidden;
    font-size: 12px;
    height: 20px;
    line-height: 20px;
    justify-content: space-between;
  }
}

.drone-card {
  .drone-title {
    margin-bottom: 8px;
  }

  .drone-info {
    margin-bottom: 8px;

    p {
      margin-bottom: 4px;
    }
  }

  .drone-actions {
    text-align: right;
  }
}

.alert-time {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.45);
  margin-top: 4px;
}

.item-group {
  padding: 20px 0 8px 24px;
  font-size: 0;

  a {
    color: rgba(0, 0, 0, 0.65);
    display: inline-block;
    font-size: 14px;
    margin-bottom: 13px;
    width: 33.33%;

    &:hover {
      color: #1890ff;
    }
  }
}
</style>
